'''
Created on 2009-11-22

@author: wangyongtao
'''
from google.appengine.api import memcache
import mokomodel
import utility
import logging

class IndexPage(utility.BaseRequestHandler):
    def get(self):
        self.response.out.write('rss')

class ModelsPage(utility.BaseRequestHandler):
    def get(self):
        cache = memcache.Client()
        page = self.pageHandler()
        limit = self.limitHandler()
        thumb = self.thumbHandler()
        offset = (page -1)*limit
        models = cache.get(str(offset), utility.CACHE_NS_GALLERY_IPHONE)
        if not models:
            models = mokomodel.model.get_topgirls_iphone(limit, offset)
            if models:
                cache.set(str(offset), models, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_GALLERY_IPHONE)
            logging.debug("not found models in cache")
        else:
            logging.debug("Load models in cache")
        count = cache.get("count", utility.CACHE_NS_GALLERY_IPHONE)
        if not count:
            count = mokomodel.model.get_topgirl_iphone_count()
            cache.set("count", count, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_GALLERY_IPHONE)
        self.rssgenerate('rss.models.xml', 
                                 {'models': models,
                                  'host':'mtopgirl.appspot.com',
                                  'totalcount':count,
                                  'page':page, 
                                  'thumb': thumb,
                                  'limit':limit,})
        
class FreePage(utility.BaseRequestHandler):
    def get(self):
        cache = memcache.Client()
        page = self.pageHandler()
        limit = self.limitHandler()
        thumb = self.thumbHandler()
        offset = (page -1)*limit
        models = cache.get(str(offset), utility.CACHE_NS_GALLERY_IPHONE)
        if not models:
            models = mokomodel.model.get_topgirls_iphone(limit, offset)
            if models:
                cache.set(str(offset), models, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_GALLERY_IPHONE)
            logging.debug("not found models in cache")
        else:
            logging.debug("Load models in cache")
        count = cache.get("count", utility.CACHE_NS_GALLERY_IPHONE)
        if not count:
            count = mokomodel.model.get_topgirl_iphone_count()
            cache.set("count", count, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_GALLERY_IPHONE)
        self.rssgenerate('rss.models.xml', 
                                 {'models': models,
                                  'host':'mtopgirl.appspot.com',
                                  'totalcount':len(models),
                                  'page':page, 
                                  'thumb':thumb,
                                  'limit':limit,})
        
class ModelPage(utility.BaseRequestHandler):
    def get(self, modelname):
        if not modelname:
            self.error(403)
            return
        if modelname.find('/') >= 0:
            splits = modelname.split('/')
            modelname = splits[0]
        cache = memcache.Client()
        model = cache.get(modelname, utility.CACHE_NS_MODEL)
        if not model:
            model = mokomodel.model.get_model(modelname)
            if model:
                cache.set(modelname, model, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_MODEL)
        if not model:
            self.error(403)
            return
        page = self.pageHandler()
        limit = self.limitHandler()
        thumb = self.thumbHandler()
        offset = (page -1)*limit
        albumskey = "album_%(name)s_%(offset)d"%{"name":modelname, "offset":offset}
        albums = cache.get(albumskey, utility.CACHE_NS_MODEL)
        if not albums:
            albums = model.get_albums_iphone(limit, offset)
            if albums:
                cache.set(albumskey, albums, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_MODEL)
        countkey = "albumcount_%s"%modelname
        count = cache.get(countkey, utility.CACHE_NS_MODEL)
        if not count:
            count = model.get_album_iphone_count()
            if count:
                cache.set(countkey, count, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_MODEL)
        self.rssgenerate('rss.model.xml', {'model':model,
                                           'albums': albums,
                                           'totalcount':count,
                                           'page':page,
                                           'thumb':thumb,
                                           'limit':limit,})
        
class AlbumPage(utility.BaseRequestHandler):
    def get(self, albumkey):
        if not albumkey:
            self.error(403)
            return
        if albumkey.find('/') >= 0:
            splits = albumkey.split('/')
            albumkey = splits[0]
        cache = memcache.Client()
        album = cache.get(albumkey, utility.CACHE_NS_ALBUM)
        if not album:
            album = mokomodel.album.get(albumkey)
            if album:
                cache.set(albumkey, album, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_ALBUM)
        if not album:
            self.error(403)
            return
        page = self.pageHandler()
        limit = self.limitHandler()
        thumb = self.thumbHandler()
        offset = (page -1)*limit
        photokey = "photo_%(album)s_%(offset)d"%{"album":albumkey, "offset":offset}
        photos = cache.get(photokey, utility.CACHE_NS_ALBUM)
        if not photos:
            photos = album.get_photos(limit, offset)
            if photos:
                cache.set(photokey, photos, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_ALBUM)
        countkey = "photocount_%s"%albumkey
        count = cache.get(countkey, utility.CACHE_NS_ALBUM)
        if not count:
            count = album.get_photo_count()
            if count:
                cache.set(countkey, count, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_ALBUM)
        self.rssgenerate('rss.album.xml', {'album':album,
                                           'photos': photos,
                                           'totalcount':count,
                                           'page':page,
                                           'thumb': thumb,
                                           'limit':limit,})
    
    
class CategoryListPage(utility.BaseRequestHandler):
    def get(self):
        cache = memcache.Client()
        page = self.pageHandler()
        limit = self.limitHandler()
        offset = (page -1)*limit
        categories = cache.get(str(offset), utility.CACHE_NS_CATEGORYLIST)
        if not categories:
            categories = mokomodel.category.get_categories(limit, offset)
            if categories:
                cache.set(str(offset), categories, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_CATEGORYLIST)
            logging.debug("not found categories in cache")
        else:
            logging.debug("Load categories in cache")
        count = cache.get("count", utility.CACHE_NS_CATEGORYLIST)
        if not count:
            count = mokomodel.category.get_category_count()
            cache.set("count", count, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_CATEGORYLIST)
        self.rssgenerate('rss.categorylist.xml', 
                                 {'categories': categories,
                                  'host':'mtopgirl.appspot.com',
                                  'totalcount':count,
                                  'page':page, 
                                  'limit':limit,})

class CategoryPage(utility.BaseRequestHandler):
    def get(self, categorykey):
        if not categorykey:
            self.error(403)
            return
        if categorykey.find('/') >= 0:
            splits = categorykey.split('/')
            categorykey = splits[0]
        cache = memcache.Client()
        catgory = cache.get(categorykey, utility.CACHE_NS_CATEGORY)
        if not catgory:
            catgory = mokomodel.category.get(categorykey)
            if catgory:
                cache.set(categorykey, catgory, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_CATEGORY)
        if not catgory:
            self.error(403)
            return
        page = self.pageHandler()
        limit = self.limitHandler()
        thumb = self.thumbHandler()
        offset = (page -1)*limit
        modelskey = '%(key)s_%(offset)s'%{'key':categorykey, 'offset':str(offset)}
        models = cache.get(modelskey, utility.CACHE_NS_CATEGORY)
        if not models:
            models = catgory.get_iphone_models(limit, offset)
            if models:
                cache.set(modelskey, models, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_CATEGORY)
            logging.debug("not found models in cache")
        else:
            logging.debug("Load models in cache")
        countkey = 'count_%s'%categorykey
        count = cache.get(countkey, utility.CACHE_NS_CATEGORY)
        if not count:
            count = catgory.get_iphone_model_count()
            cache.set(countkey, count, time=utility.CACHE_DURATION, namespace=utility.CACHE_NS_CATEGORY)
        self.rssgenerate('rss.models.xml', 
                                 {'models': models,
                                  'host':'mtopgirl.appspot.com',
                                  'totalcount':count,
                                  'page':page,
                                  'thumb':thumb, 
                                  'limit':limit,})    